package com.yanfan.community.building.controller;

import cn.hutool.core.collection.CollUtil;
import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.TreeSelect;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.community.building.domain.BuildingTreeSelect;
import com.yanfan.community.building.domain.CommunityBuilding;
import com.yanfan.community.building.service.ICommunityBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 建筑Controller
 *
 * @author li
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/community/building")
public class CommunityBuildingController extends BaseController {
    @Autowired
    private ICommunityBuildingService sysBuildingService;

    /**
     * 查询建筑列表
     */
//@PreAuthorize("@ss.hasPermi('community:building:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunityBuilding communityBuilding) {
        startPage();
        List<CommunityBuilding> list = sysBuildingService.selectSysBuildingList(communityBuilding);
        return getDataTable(list);
    }

    /**
     * 导出建筑列表
     */
    //@PreAuthorize("@ss.hasPermi('community:building:export')")
    @Log(title = "建筑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunityBuilding communityBuilding) {
        List<CommunityBuilding> list = sysBuildingService.selectSysBuildingList(communityBuilding);
        ExcelUtil<CommunityBuilding> util = new ExcelUtil<CommunityBuilding>(CommunityBuilding.class);
        util.exportExcel(response, list, "建筑数据");
    }

    /**
     * 获取建筑详细信息
     */
    //@PreAuthorize("@ss.hasPermi('community:building:query')")
    @GetMapping(value = "/{buildingId}")
    public AjaxResult getInfo(@PathVariable("buildingId") Long buildingId) {
        return AjaxResult.success(sysBuildingService.selectSysBuildingByBuildingId(buildingId));
    }

    /**
     * 新增建筑
     */
    //@PreAuthorize("@ss.hasPermi('community:building:add')")
    @Log(title = "建筑", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunityBuilding communityBuilding) {
        return toAjax(sysBuildingService.insertSysBuilding(communityBuilding));
    }

    /**
     * 修改建筑
     */
    //@PreAuthorize("@ss.hasPermi('community:building:edit')")
    @Log(title = "建筑", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommunityBuilding communityBuilding) {
        return toAjax(sysBuildingService.updateSysBuilding(communityBuilding));
    }

    /**
     * 删除建筑
     */
    //@PreAuthorize("@ss.hasPermi('community:building:remove')")
    @Log(title = "建筑", businessType = BusinessType.DELETE)
    @DeleteMapping("/{buildingIds}")
    public AjaxResult remove(@PathVariable Long[] buildingIds) {
        return toAjax(sysBuildingService.deleteSysBuildingByBuildingIds(buildingIds));
    }


    /**
     * 建筑树
     */
    //@PreAuthorize("@ss.hasPermi('community:building:remove')")
//    @Log(title = "建筑", businessType = BusinessType.DELETE)
    @GetMapping("/buildingAll")
    public AjaxResult buildingAll(CommunityBuilding communityBuilding) {
        List<CommunityBuilding> list = sysBuildingService.selectSysBuildingList(communityBuilding);
        if (CollUtil.isEmpty(list)) {
            return success(CollUtil.newArrayList());
        }
        return success(list);
    }

    @GetMapping("/buildingTree")
    public AjaxResult buildingTree(CommunityBuilding communityBuilding) {
        List<BuildingTreeSelect> list = sysBuildingService.buildingTree(communityBuilding);
        return success(list);
    }


}
